package com.imooc.security.demo.controller;

import com.imooc.security.demo.exception.UserNotExistException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 不处理HTTP请求，只处理其他Controller抛出的异常
 * @author JackYang
 * @description:
 * @create: 2020/6/13 22:33
 */
@ControllerAdvice
public class ControllerExceptionHandler {
    
    @ExceptionHandler(UserNotExistException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String, String> handleException(UserNotExistException exception ){
        HashMap<String, String> map = new HashMap<>();
        map.put("message",exception.getMessage());
        map.put("time",new Date().toString());
        return map;
    }
}
